
<form id="test-form" lay-filter="test-form" class="layui-form model-form">
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input name="title" placeholder="请输入标题" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <input name="content" placeholder="请输入内容" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>
 
 
 
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="test-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form', 'formSelects'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var formSelects = layui.formSelects;

 

        var url = '/test/add';
        // 回显user数据
        var test = admin.getTempData('t_test');
        if (test) {         
            url = '/test/update';
            form.val('test-form', test);         
        }

        // 表单提交事件
        form.on('submit(test-form-submit)', function (data) {
            layer.load(2);
            admin.go(url, data.field, function (data) {
                layer.closeAll('loading');
                if (data.code == 200) {
                    layer.msg(data.msg, {icon: 1});
                    admin.finishPopupCenter();
                }else if(data.code == 401){
                	layer.msg(data.msg, {icon: 2, time: 1500}, function () {location.replace('/login'); }, 1000);
                 	 return;
                 } else {
                	console.log('err:'+data.code);
                    layer.msg(data.msg, {icon: 2});
                }
            });
            return false;
        });
    });
</script>